﻿using UnityEngine;
using System.Collections;

[System.Serializable]
public class CharacterOption
{
	public string information;
	public string character;
	public string characterStance;
	public int price;
	public bool isBought;
	public UISprite boughtPicture;
	
	public int playerPrefIndex; 
}

public class CharacterShop : MonoBehaviour {
	
	public CharacterOption[] characters;
	public UISprite targetSprite;
	public UISprite characterStance;
	public UISprite buyButton;
	public UILabel coinLabel;
	int boughtCharacters;
	public string selectedSprite;
	public UILabel InsufficientFund;
	
	public UISprite priceBlocker;

	public UIPanel characterPanel;
	public UIPanel menuPanel;

	bool displayInsufFund = false;

	DataScript data;

	public void Start()
	{
		//PlayerPrefs.DeleteAll();
		//Debug.Log("CharacterName: " + DataScript.GetPlayerName());
		//PlayerPrefs.DeleteKey("boughtCharacters");
		//PlayerPrefs.DeleteKey("savedCoins");
		coinLabel.text = string.Format("{0:#,###0}", DataScript.AccessCoin());
		for(int i = 0; i < characters.Length; i++)
		{
			characters[i].playerPrefIndex = i;
			
			if(DataScript.IsCharacterBought(i) != 0)
			{
				characters[i].isBought = true;
				characters[i].boughtPicture.alpha = 1.0f;
			}
			else
			{
				characters[i].boughtPicture.alpha = 0.0f;
			}
		}
		selectedSprite = "btn05_02";
		data = GameObject.FindGameObjectWithTag("DataManager").GetComponent<DataScript>();
		setPriceBlocker();

		if(characters[0].isBought)
			buyButton.spriteName = "btn_select";
		else
			buyButton.spriteName = "btn_buy";

	}

	bool charPanelNotRefreshed = true;
	
	void Update()
	{
		//characterPanel.Refresh();
		if(displayInsufFund)
		{
			InsufficientFund.alpha -= Time.deltaTime;
			if(InsufficientFund.alpha <= 0)
			{
				InsufficientFund.alpha = 0;
				displayInsufFund = false;
			}
		}

		if(charPanelNotRefreshed && characterPanel.transform.localPosition.x < -1400)
		{
			Debug.Log("REFRESH");
			characterPanel.Refresh();
			charPanelNotRefreshed = false;
		}
		//Debug.Log("AW");
		if(Input.GetKey(KeyCode.X))
		{
			if(Input.GetKeyDown(KeyCode.C))
			{
				//Debug.Log("AW");
				//data.coins += 1000;
				DataScript.SetCoins(DataScript.AccessCoin() + 1000);
				coinLabel.text = string.Format("{0:#,###0}", DataScript.AccessCoin());
			}
		}
	}

	//void LateUpdate()
	//{
		//NGUITools.Broadcast("Refresh");
	//}

	void setPriceBlocker()
	{
		for(int i = 0; i < characters.Length; i++)
		{
			if(characters[i].character == selectedSprite)
			{
				if(characters[i].isBought)
					priceBlocker.alpha = 1.0f;
				else
					priceBlocker.alpha = 0.0f;
			}
		}
	}
	
	public void BackTOLobby()
	{
		Application.LoadLevel("LobbyScene");
	}
	public void BuyCharacter()
	{
		for(int i =0 ; i < characters.Length;i++)
		{
			if(selectedSprite == characters[i].character)
			{
				CharacterOption tmpChar = characters[i];
				if(tmpChar.isBought)
				{
					displayInsufFund = true;
					InsufficientFund.text = ((CHARACTERS)characters[i].playerPrefIndex).ToString() + " IS SELECTED.";
					InsufficientFund.alpha = 1.5f;

					DataScript.SetCharacter((CHARACTERS)characters[i].playerPrefIndex);
					GameObject.FindGameObjectWithTag("DataManager").GetComponent<DataScript>().selectedCharacter = (CHARACTERS)characters[i].playerPrefIndex;
					return;
				}
				
				int currentCoins = DataScript.AccessCoin();
				int totalCoins = currentCoins - tmpChar.price;
				
				if(totalCoins >= 0)
				{
					buyButton.spriteName = "btn_select";
					tmpChar.isBought = true;
					tmpChar.boughtPicture.alpha = 1.0f;
					DataScript.SetCoins(totalCoins);
					DataScript.SetCharacterAsBought(i);
					coinLabel.text = string.Format("{0:#,###0}", totalCoins);
					setPriceBlocker();
					PlayerPrefs.Save();
					characterPanel.Refresh();
				}
				else
				{
					displayInsufFund = true;
					InsufficientFund.text = "Insufficient Funds!";
					InsufficientFund.alpha = 1.5f;
				}
				return;
			}
		}
	}
	
	public void SetInfo()
	{
		UISprite tmpSprite = UIButton.current.tweenTarget.GetComponent<UISprite>();
		string _name = selectedSprite = tmpSprite.spriteName;
		
		for(int i = 0; i < characters.Length; i++)
		{
			if(_name == characters[i].character)
			{
				if(characters[i].isBought)
					buyButton.spriteName = "btn_select";
				else
					buyButton.spriteName = "btn_buy";
				
				setPriceBlocker();
				targetSprite.spriteName = characters[i].information;
				characterStance.spriteName = characters[i].characterStance;
				return;
			}
		}
	}
}
